package mao.leetcode.others;

import java.util.ArrayList;
import java.util.List;

public class leetcode275 {
    List<String> ans = new ArrayList<>();
    StringBuilder builder = new StringBuilder();
    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) return ans;
        if (root.left == null && root.right == null){
            ans.add(String.valueOf(root.val));
            return ans;
        }
        builder.append(root.val);
        helper(root.left);
        helper(root.right );
        return ans;
    }
    public void helper(TreeNode root){
        if (root == null) return;
        int oldLength = builder.length();
        builder.append("->").append(root.val);
        if (root.left == null && root.right == null) {
            ans.add(builder.toString());
        }else {
            helper(root.left);
            helper(root.right);
        }
        builder.delete(oldLength,builder.length());
    }

    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
    }
}
